package com.vanvalt.payhui.modules.pay.api.business;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.vanvalt.payhui.modules.pay.api.command.impl.PageCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.RequestCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.ResponseCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.UserShareCommand;
import com.vanvalt.payhui.modules.pay.api.util.ErrorCode;
import com.vanvalt.payhui.modules.pay.entity.UserShare;
import com.vanvalt.payhui.modules.pay.service.UserService;
import com.vanvalt.payhui.modules.pay.service.UserShareService;
import com.vanvalt.payhui.util.constat.Constant;
import com.vanvalt.payhui.util.json.JsonConversion;

/**
 * 用户分享
 */
public class UserShareProcess extends BaseProcess {
	/**
	 *日志.
	 */
	private static Logger 				logger = Logger.getLogger(UserShareProcess.class);
	/*private static HttpServletRequest	httpServletRequest	= ((ServletRequestAttributes) RequestContextHolder
			.getRequestAttributes()).getRequest();*/
	
	private UserService userService;
	private UserShareService userShareService;
	
	/**
	 * 构造方法.
	 */	
	public UserShareProcess() {
		super();
	}

	/**
	 * @param userShareService
	 */
	public UserShareProcess(UserShareService userShareService){
		super();
		this.userShareService = userShareService;
	}
	
	/**
	 * @param userService
	 * @param userShareService
	 */
	public UserShareProcess(UserService userService, UserShareService userShareService){
		super();
		this.userService = userService;
		this.userShareService = userShareService;
	}

	/**
	 * 用户分享记录列表
	 * @param pageCommand
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public ResponseCommand userShareList(PageCommand pageCommand, RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException {
		
		// 分页参数
		pageCommand = initPagecommand(pageCommand, requestCommand);
		RowBounds rowBounds = new RowBounds(pageCommand.getPage().getOffset(), 
				pageCommand.getPage().getPageSize());
				
		UserShareCommand userShareCommand = new UserShareCommand(requestCommand.getObject().toString());
		Map<String, Object> map = userShareCommand.getMap();
		
		String uid = (String) map.get("uid");
        String currentPage = (String) map.get("currentPage");
        String pageSize = (String) map.get("pageSize");
		
        Map<String,Object> resultMap = Maps.newHashMap();
		List<Map<String,Object>> resultList = Lists.newArrayList();
		
		int total = 0;
		
		if(map.isEmpty() && StringUtils.isBlank(uid) && StringUtils.isBlank(currentPage) && StringUtils.isBlank(pageSize)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	User user = userService.get(uid);
        	
        	if(user == null){
        		return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        	} else {
        		
        		/*Integer currentPages = Integer.parseInt(currentPage);
                Integer lineSize = Integer.parseInt(pageSize);*/
                
                UserShare usParam = new UserShare();
                usParam.setDelFlag(Constant.DEL_FLAG_NORMAL);
                usParam.setInviterId(uid);
                usParam.setOffset(rowBounds.getOffset());
                usParam.setLimit(rowBounds.getLimit());
                
                long totalCount = userShareService.findCount(usParam);
                total = (new Long(totalCount).intValue());
                
                /*Page<UserShare> page = userShareService.findPage(new Page<UserShare>(currentPages, lineSize), usParam); 
                List<UserShare> userShareList = page.getList();*/
                List<UserShare> userShareList = userShareService.findListByPage(usParam);
                
                for(UserShare us:userShareList){
                	
                	Map<String,Object> userShareMap = Maps.newHashMap();
                	
                	userShareMap.put("name", us.getName());
                	userShareMap.put("phone", StringUtils.hidePhone(us.getPhone())); // 隐藏手机号码中间4位
                	userShareMap.put("registrationTime", us.getRegistrationTime());
                	
                	resultList.add(userShareMap);
                }
        	}
        }
		
		resultMap.put("totalCount", total);
		resultMap.put("shareRecords", resultList);
		
		return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
 				JsonConversion.writeMapJSON(resultMap), responseCommand);
	}
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserShareService getUserShareService() {
		return userShareService;
	}

	public void setUserShareService(UserShareService userShareService) {
		this.userShareService = userShareService;
	}

}
